import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import '../../helper/channelHelper.dart';
import '../../helper/routeHelper.dart';
import '../../model/station.dart';
import 'ditus/liuyu.dart';

class GMapHelper {
  //谷歌影像底图
  static LayerOptions TileGooleMap() {
    return TileLayerOptions(
        urlTemplate:
            "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}");
  }

  static LayerOptions TileWhiteMap() {
    return TileLayerOptions(
      urlTemplate:
          "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}",
    );
  }

  static LayerOptions TileShuiXiMap() {
    return TileLayerOptions(
      urlTemplate:
          "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/WorldHydroMap/MapServer/tile/{z}/{y}/{x}",
    );
  }

  static List<LayerOptions> NewLayerList() {
    return <LayerOptions>[];
  }

  static List<LayerOptions> DituLayer = DituLayerList();

  static List<LayerOptions> DituLayerList() {
    var dituList = <LayerOptions>[];
    dituList.add(getDituLayerOptions3(LiuYu, Colors.deepOrange));
    return dituList;
  }

  static LayerOptions getDituLayerOptions(List<List<double>> pp, Color color) {
    List<LatLng> points = <LatLng>[];

    for (int i = 0; i < pp.length; i++) {
      var p = new LatLng(pp[i][0], pp[i][1]);
      points.add(p);
    }

    return PolygonLayer(points, 2, color);
  }

  static LayerOptions getDituLayerOptions3(List<List<double>> pp, Color color) {
    List<LatLng> points = <LatLng>[];

    for (int i = 0; i < pp.length; i++) {
      var p = new LatLng(pp[i][0], pp[i][1]);
      points.add(p);
    }

    return PolylineLayer(points, 2, color);
  }

  static List<LayerOptions> getDituLayerOptions2(
      List<List<List<double>>> pps, Color color) {
    var xList = <LayerOptions>[];

    for (int j = 0; j < pps.length; j++) {
      var pp = pps[j];
      List<LatLng> points = <LatLng>[];
      for (int i = 0; i < pp.length; i++) {
        var p = new LatLng(pp[i][0], pp[i][1]);
        points.add(p);
      }

      xList.add(PolylineLayer(points, 1, Colors.blue));
    }

    return xList;
  }

  static List<LayerOptions> getStationLayer(
      List<Station> pps, BuildContext ctx) {
    var xList = <LayerOptions>[];

    for (int j = 0; j < pps.length; j++) {
      var p = new LatLng(pps[j].LTTD, pps[j].LGTD);

      xList.add(MyMarker2(
          p, pps[j].STNM, () => RouteHelper.StationInfoPage(ctx, pps[j])));
    }

    return xList;
  }

  static List<LayerOptions> getStationLayer2(
      List<Station> pps, BuildContext ctx) {
    var xList = <LayerOptions>[];

    for (int j = 0; j < pps.length; j++) {
      var p = new LatLng(pps[j].LTTD, pps[j].LGTD);

      xList.add(MyMarker2(
          p, pps[j].STNM, () => RouteHelper.StationInfoPage(ctx, pps[j])));
    }

    return xList;
  }

  static LayerOptions MyLocation(LatLng location) {
    return MarkerLayer(location, Icons.my_location, Colors.red);
  }

  static LayerOptions MyMarker(LatLng location) {
    return MarkerLayer(location, Icons.location_on, Colors.blue);
  }

  static LayerOptions MyMarker2(
      LatLng location, String st, VoidCallback onpressed) {
    return MarkerLayer3(
        location, Icons.location_on, Colors.blue, st, onpressed);
  }

  static LatLng getLocation(String xx) {
    double x1, x2;
    x1 = double.parse(xx.split(',')[0]);
    x2 = double.parse(xx.split(',')[1]);

    return LatLng(x1, x2);
  }

  static String getLocationStr(LatLng xx) {
    return xx.latitude.toString() + "," + xx.longitude.toString();
  }

  static LayerOptions MarkerLayer(LatLng location, IconData icon, Color color) {
    return new MarkerLayerOptions(markers: [
      new Marker(
        width: 36.0,
        height: 36.0,
        point: location,
        builder: (ctx) => new Container(
          child: Center(
            child: Icon(
              icon,
              color: color,
              size: 24.0,
            ),
          ),
          // child: Text(location.toString(),style: TextStyle(color: Colors.white),),
        ),
      )
    ]);
  }

  static LayerOptions MarkerLayer3(LatLng location, IconData icon, Color color,
      String st, VoidCallback onpressed) {
    return new MarkerLayerOptions(markers: [
      new Marker(
        width: 36.0,
        height: 36.0,
        point: location,
        builder: (ctx) => new Container(
          child: Center(
            child: IconButton(
              icon: Icon(
                icon,
                color: color,
                size: 24.0,
              ),
              onPressed: onpressed,
              tooltip: st,
            ),
          ),
          // child: Text(location.toString(),style: TextStyle(color: Colors.white),),
        ),
      )
    ]);
  }

  static LayerOptions BJLayer(
      LatLng location, double w1, double h1, Widget child) {
    return new MarkerLayerOptions(markers: [
      new Marker(
        width: w1,
        height: h1,
        point: location,
        builder: (ctx) => child,
      )
    ]);
  }

  static LayerOptions MarkerLayer2(LatLng location, IconData icon, Color color,
      String title, String content) {
    return new MarkerLayerOptions(markers: [
      new Marker(
        width: 36.0,
        height: 36.0,
        point: location,
        builder: (ctx) => new Container(
          child: Center(
              child: GestureDetector(
            onTap: () => ChannelHelper.showToast(content),
            child: Icon(
              icon,
              color: color,
              size: 24.0,
            ),
          )),
          // child: Text(location.toString(),style: TextStyle(color: Colors.white),),
        ),
      )
    ]);
  }

  static LayerOptions PolylineLayer(
      List<LatLng> points, double lineWidth, Color color) {
    return new PolylineLayerOptions(
      polylines: [
        new Polyline(points: points, strokeWidth: lineWidth, color: color),
      ],
    );
  }

  static LayerOptions PolygonLayer(
      List<LatLng> points, double lineWidth, Color color) {
    return new PolygonLayerOptions(
      polygons: [
        new Polygon(points: points, color: color),
      ],
    );
  }
}
